package com.xinxin.blockchain.common.config;

import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author ys
 * @date 2021/5/18
 */
@Configuration
@MapperScan("com.xinxin.blockchain.mapper")
public class MybatisPlusConfig {

    /**
     * Mybatis Plus 分页插件
     */
    // @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 攻击 SQL 阻断解析器、加入解析链
        paginationInterceptor.setSqlParserList(Lists.newArrayList(new BlockAttackSqlParser()));
        return paginationInterceptor;
    }

    /**
     * PageHelper 分页插件
     * 与 PaginationInterceptor 同时开启时有BUG，会变成 LIMIT 0 OFFSET 10
     * 可以使用 pagehelper-spring-boot-starter 来同时开启
     */
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();

        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        // 配置mysql数据库的方言
        properties.setProperty("dialect", "mysql");
        // 当pageSize=0时查询全部结果
        properties.setProperty("pageSizeZero", "true");

        pageHelper.setProperties(properties);
        return pageHelper;
    }
}
